#####################################
##REPLICATION FOR MCGHEE, ET AL.,####
##"A PRIMARY CAUSE OF PARTISANSHIP"##
#####################################

##Table A4 (Appendix)##

#Load Data#
library(Zelig)
library(sandwich)
setwd("XXXX") #ENTER DIRECTORY PATHNAME IN PLACE OF XXXX

legyrd <- read.csv("legislator primaries.dem.csv", header=TRUE)
legyrr <- read.csv("legislator primaries.rep.csv", header=TRUE)

#generate election year counter#
legyrd$yrcnt <- (legyrd$elec-1992)/2
legyrr$yrcnt <- (legyrr$elec-1992)/2

#drop ideowt var, which is not used for this table#
legyrd$ideowt <- NULL
legyrr$ideowt <- NULL

#drop missing data#
legyrd <- na.omit(legyrd)
legyrr <- na.omit(legyrr)

#mean-deviate the presidential vote#
legyrd$pvtmn <- legyrd$pvote - mean(legyrd$pvote)
legyrr$pvtmn <- legyrr$pvote - mean(legyrr$pvote)

#define years & states as factors for FE regression#
legyrd$icpsrst <- as.factor(legyrd$icpsrst)
legyrd$yrcnt <- as.factor(legyrd$yrcnt)
legyrr$icpsrst <- as.factor(legyrr$icpsrst)
legyrr$yrcnt <- as.factor(legyrr$yrcnt)

#Democrats#
z.out <- zelig(pred_np ~ semicl + semiop + open + nonpart + pvtmn + 
	I(semicl*pvtmn) + I(semiop*pvtmn) + I(open*pvtmn) + I(nonpart*pvtmn) +
	yrcnt + icpsrst, 
	model="ls", robust=list(method="vcovHAC"), data=as.data.frame(legyrd))
summary(z.out)
length(legyrd$icpsrst)
minp <- min(legyrd$pvtmn)
maxp <- max(legyrd$pvtmn)
x.out0 <- setx(z.out, semicl=0, semiop=0, open=0, nonpart=0, pvtmn=minp)
x.out1 <- setx(z.out, semicl=0, semiop=0, open=0, nonpart=0, pvtmn=maxp)
s.cl.d <- sim(z.out, x=x.out0, x1=x.out1)
x.out0 <- setx(z.out, semicl=1, semiop=0, open=0, nonpart=0, pvtmn=minp)
x.out1 <- setx(z.out, semicl=1, semiop=0, open=0, nonpart=0, pvtmn=maxp)
s.scl.d <- sim(z.out, x=x.out0, x1=x.out1)
x.out0 <- setx(z.out, semicl=0, semiop=1, open=0, nonpart=0, pvtmn=minp)
x.out1 <- setx(z.out, semicl=0, semiop=1, open=0, nonpart=0, pvtmn=maxp)
s.sop.d <- sim(z.out, x=x.out0, x1=x.out1)
x.out0 <- setx(z.out, semicl=0, semiop=0, open=1, nonpart=0, pvtmn=minp)
x.out1 <- setx(z.out, semicl=0, semiop=0, open=1, nonpart=0, pvtmn=maxp)
s.op.d <- sim(z.out, x=x.out0, x1=x.out1)
x.out0 <- setx(z.out, semicl=0, semiop=0, open=0, nonpart=1, pvtmn=minp)
x.out1 <- setx(z.out, semicl=0, semiop=0, open=0, nonpart=1, pvtmn=maxp)
s.np.d <- sim(z.out, x=x.out0, x1=x.out1)

#Republicans#
z.out <- zelig(pred_np ~ semicl + semiop + open + nonpart + pvtmn + 
	I(semicl*pvtmn) + I(semiop*pvtmn) + I(open*pvtmn) + I(nonpart*pvtmn) +
	yrcnt + icpsrst, 
	model="ls", robust=list(method="vcovHAC"), data=as.data.frame(legyrr))
summary(z.out)
length(legyrr$icpsrst)
minp <- min(legyrr$pvtmn)
maxp <- max(legyrr$pvtmn)
x.out0 <- setx(z.out, semicl=0, semiop=0, open=0, nonpart=0, pvtmn=minp)
x.out1 <- setx(z.out, semicl=0, semiop=0, open=0, nonpart=0, pvtmn=maxp)
s.cl.r <- sim(z.out, x=x.out0, x1=x.out1)
x.out0 <- setx(z.out, semicl=1, semiop=0, open=0, nonpart=0, pvtmn=minp)
x.out1 <- setx(z.out, semicl=1, semiop=0, open=0, nonpart=0, pvtmn=maxp)
s.scl.r <- sim(z.out, x=x.out0, x1=x.out1)
x.out0 <- setx(z.out, semicl=0, semiop=1, open=0, nonpart=0, pvtmn=minp)
x.out1 <- setx(z.out, semicl=0, semiop=1, open=0, nonpart=0, pvtmn=maxp)
s.sop.r <- sim(z.out, x=x.out0, x1=x.out1)
x.out0 <- setx(z.out, semicl=0, semiop=0, open=1, nonpart=0, pvtmn=minp)
x.out1 <- setx(z.out, semicl=0, semiop=0, open=1, nonpart=0, pvtmn=maxp)
s.op.r <- sim(z.out, x=x.out0, x1=x.out1)
x.out0 <- setx(z.out, semicl=0, semiop=0, open=0, nonpart=1, pvtmn=minp)
x.out1 <- setx(z.out, semicl=0, semiop=0, open=0, nonpart=1, pvtmn=maxp)
s.np.r <- sim(z.out, x=x.out0, x1=x.out1)

##Figure 4##
close.screen(all.screens=TRUE)
#Closed: Dem
par(cex.axis=.8, cex.lab=.8, font.axis=6, lheight=.5, mar=c(1,3,1.5,.5), mgp=c(1,.75,0),
	fig=c(0,.36,.55,.98), new=TRUE)
closed.d <- array(c(mean(s.cl.d$qi$pv1)+mean(s.cl.d$qi$fd), mean(s.cl.d$qi$pv1)))
p <- array(c(min(legyrd$pvtmn),max(legyrd$pvtmn)))
plot(p, closed.d, type="l", lty="dashed", lwd=2, ylim=c(-1.5, 1.5),
		xlab="", ylab="", xaxt="n")
		#lab=c(8,5,4)
closed.d.p <- array(c(mean(s.cl.d$qi$pv1)+2*sd(s.cl.d$qi$pv1)+
						mean(s.cl.d$qi$fd)+2*sd(s.cl.d$qi$fd),
						mean(s.cl.d$qi$pv1)+2*sd(s.cl.d$qi$pv1)))
lines(p, closed.d.p, type="l", lty="dashed", ylim=c(-1.5, 1.5),
	xlab="", ylab="", lab=c(8,5,4), col="gray")
closed.d.m <- array(c(mean(s.cl.d$qi$pv1)-2*sd(s.cl.d$qi$pv1)+
						mean(s.cl.d$qi$fd)-2*sd(s.cl.d$qi$fd),
						mean(s.cl.d$qi$pv1)-2*sd(s.cl.d$qi$pv1)))
lines(p, closed.d.m, type="l", lty="dashed", ylim=c(-1.5, 1.5),
	xlab="", ylab="", lab=c(8,5,4), col="gray")
#Closed: Rep
closed.r <- array(c(mean(s.cl.r$qi$pv1), mean(s.cl.r$qi$pv1)+mean(s.cl.r$qi$fd)))
lines(p, closed.r, type="l", lty="solid", lwd=2, ylim=c(-1.5, 1.5),
		xlab="", ylab="", lab=c(8,5,4))
closed.r.p <- array(c(mean(s.cl.r$qi$pv1)+2*sd(s.cl.r$qi$pv1),
	mean(s.cl.r$qi$pv1)+2*sd(s.cl.r$qi$pv1)+mean(s.cl.r$qi$fd)+2*sd(s.cl.r$qi$fd)))
lines(p, closed.r.p, type="l", lty="solid", ylim=c(-1.5, 1.5),
	xlab="", ylab="", lab=c(8,5,4), col="gray")
closed.r.m <- array(c(mean(s.cl.r$qi$pv1)-2*sd(s.cl.r$qi$pv1),
	mean(s.cl.r$qi$pv1)-2*sd(s.cl.r$qi$pv1)+mean(s.cl.r$qi$fd)-2*sd(s.cl.r$qi$fd)))
lines(p, closed.r.m, type="l", lty="solid", ylim=c(-1.5, 1.5),
	xlab="", ylab="", lab=c(8,5,4), col="gray")
abline(h=0, lty="solid")
title(main="Pure Closed", font.main=7, cex=1.5)
mtext("Least", side=1, line=.3, cex=.8, at=c(-0.38), font=6)
mtext("Most", side=1, line=.3, cex=.8, at=c(0.35), font=6)
mtext("Predicted Ideal Point", side=2, line=1.8, cex=1.5, at=c(-2.0), font=6)
text(-0.3,1.0,"Republicans", cex=.95, adj=c(0,0), font=6)
text(0,-1.1,"Democrats", cex=.95, adj=c(0,1), font=6)
#Semi-closed: Dem
par(cex.axis=.8, cex.lab=.8, font.axis=6, lheight=.5, mar=c(1,1.5,1.5,.5), mgp=c(1,.75,0),
	fig=c(.36,.68,.55,.98), new=TRUE)
semiclosed.d <- array(c(mean(s.scl.d$qi$pv1)+mean(s.scl.d$qi$fd), mean(s.scl.d$qi$pv1)))
plot(p, semiclosed.d, type="l", lty="dashed", lwd=2, ylim=c(-1.5, 1.5),
		xlab="", ylab="", xaxt="n")
semiclosed.d.p <- array(c(mean(s.scl.d$qi$pv1)+2*sd(s.scl.d$qi$pv1)+
							mean(s.scl.d$qi$fd)+2*sd(s.scl.d$qi$fd),
							mean(s.scl.d$qi$pv1)+2*sd(s.scl.d$qi$pv1)))
lines(p, semiclosed.d.p, type="l", lty="dashed", ylim=c(-1.5, 1.5),
	xlab="", ylab="", lab=c(8,5,4), col="gray")
semiclosed.d.m <- array(c(mean(s.scl.d$qi$pv1)-2*sd(s.scl.d$qi$pv1)+
							mean(s.scl.d$qi$fd)-2*sd(s.scl.d$qi$fd),
							mean(s.scl.d$qi$pv1)-2*sd(s.scl.d$qi$pv1)))
lines(p, semiclosed.d.m, type="l", lty="dashed", ylim=c(-1.5, 1.5),
	xlab="", ylab="", lab=c(8,5,4), col="gray")
#Semi-closed: Rep
semiclosed.r <- array(c(mean(s.scl.r$qi$pv1), mean(s.scl.r$qi$pv1)+mean(s.scl.r$qi$fd)))
lines(p, semiclosed.r, type="l", lty="solid", lwd=2, ylim=c(-1.5, 1.5),
		xlab="", ylab="", lab=c(8,5,4))
semiclosed.r.p <- array(c(mean(s.scl.r$qi$pv1)+2*sd(s.scl.r$qi$pv1),
	mean(s.scl.r$qi$pv1)+2*sd(s.scl.r$qi$pv1)+mean(s.scl.r$qi$fd)+2*sd(s.scl.r$qi$fd)))
lines(p, semiclosed.r.p, type="l", lty="solid", ylim=c(-1.5, 1.5),
	xlab="", ylab="", lab=c(8,5,4), col="gray")
semiclosed.r.m <- array(c(mean(s.scl.r$qi$pv1)-2*sd(s.scl.r$qi$pv1),
	mean(s.scl.r$qi$pv1)-2*sd(s.scl.r$qi$pv1)+mean(s.scl.r$qi$fd)-2*sd(s.scl.r$qi$fd)))
lines(p, semiclosed.r.m, type="l", lty="solid", ylim=c(-1.5, 1.5),
	xlab="", ylab="", lab=c(8,5,4), col="gray")
abline(h=0, lty="solid")
title(main="Semi-Closed", font.main=7, cex=1.5)
mtext("Least", side=1, line=.3, cex=.8, at=c(-0.38), font=6)
mtext("Most", side=1, line=.3, cex=.8, at=c(0.35), font=6)
text(-0.3,1.15,"Republicans", cex=.95, adj=c(0,0), font=6)
text(0,-1.2,"Democrats", cex=.95, adj=c(0,1), font=6)
#Semi-open: Dem
par(cex.axis=.8, cex.lab=.8, font.axis=6, lheight=.5, mar=c(1,1.5,1.5,.5), mgp=c(1,.75,0),
	fig=c(.68,.99,.55,.98), new=TRUE)
semiopen.d <- array(c(mean(s.sop.d$qi$pv1)+mean(s.sop.d$qi$fd), mean(s.sop.d$qi$pv1)))
plot(p, semiopen.d, type="l", lty="dashed", lwd=2, ylim=c(-1.5, 1.5),
		xlab="", ylab="", xaxt="n")
semiopen.d.p <- array(c(mean(s.sop.d$qi$pv1)+2*sd(s.sop.d$qi$pv1)+
						mean(s.sop.d$qi$fd)+2*sd(s.sop.d$qi$fd),
						mean(s.sop.d$qi$pv1)+2*sd(s.sop.d$qi$pv1)))
lines(p, semiopen.d.p, type="l", lty="dashed", ylim=c(-1.5, 1.5),
	xlab="", ylab="", lab=c(8,5,4), col="gray")
semiopen.d.m <- array(c(mean(s.sop.d$qi$pv1)-2*sd(s.sop.d$qi$pv1)+
						mean(s.sop.d$qi$fd)-2*sd(s.sop.d$qi$fd),
						mean(s.sop.d$qi$pv1)-2*sd(s.sop.d$qi$pv1)))
lines(p, semiopen.d.m, type="l", lty="dashed", ylim=c(-1.5, 1.5),
	xlab="", ylab="", lab=c(8,5,4), col="gray")
#Semi-open: Rep
semiopen.r <- array(c(mean(s.sop.r$qi$pv1), mean(s.sop.r$qi$pv1)+mean(s.sop.r$qi$fd)))
lines(p, semiopen.r, type="l", lty="solid", lwd=2, ylim=c(-1.5, 1.5),
		xlab="", ylab="", lab=c(8,5,4))
semiopen.r.p <- array(c(mean(s.sop.r$qi$pv1)+2*sd(s.sop.r$qi$pv1),
	mean(s.sop.r$qi$pv1)+2*sd(s.sop.r$qi$pv1)+mean(s.sop.r$qi$fd)+2*sd(s.sop.r$qi$fd)))
lines(p, semiopen.r.p, type="l", lty="solid", ylim=c(-1.5, 1.5),
	xlab="", ylab="", lab=c(8,5,4), col="gray")
semiopen.r.m <- array(c(mean(s.sop.r$qi$pv1)-2*sd(s.sop.r$qi$pv1),
	mean(s.sop.r$qi$pv1)-2*sd(s.sop.r$qi$pv1)+mean(s.sop.r$qi$fd)-2*sd(s.sop.r$qi$fd)))
lines(p, semiopen.r.m, type="l", lty="solid", ylim=c(-1.5, 1.5),
	xlab="", ylab="", lab=c(8,5,4), col="gray")
abline(h=0, lty="solid")
title(main="Semi-open", font.main=7, cex=1.5)
mtext("Least", side=1, line=.3, cex=.8, at=c(-0.38), font=6)
mtext("Most", side=1, line=.3, cex=.8, at=c(0.35), font=6)
text(-0.3,1.1,"Republicans", cex=.95, adj=c(0,0), font=6)
text(-0.15,-1.1,"Democrats", cex=.95, adj=c(0,1), font=6)
#Open: Dem
par(cex.axis=.8, cex.lab=.8, font.axis=6, lheight=.5, mar=c(1,3,1.5,.5), mgp=c(1,.75,0),
	fig=c(0,.36,.1,.53), new=TRUE)
open.d <- array(c(mean(s.op.d$qi$pv1)+mean(s.op.d$qi$fd), mean(s.op.d$qi$pv1)))
plot(p, open.d, type="l", lty="dashed", lwd=2, ylim=c(-1.5, 1.5),
		xlab="", ylab="", xaxt="n")
open.d.p <- array(c(mean(s.op.d$qi$pv1)+2*sd(s.op.d$qi$pv1)+
					mean(s.op.d$qi$fd)+2*sd(s.op.d$qi$fd),
					mean(s.op.d$qi$pv1)+2*sd(s.op.d$qi$pv1)))
lines(p, open.d.p, type="l", lty="dashed", ylim=c(-1.5, 1.5),
	xlab="", ylab="", lab=c(8,5,4), col="gray")
open.d.m <- array(c(mean(s.op.d$qi$pv1)-2*sd(s.op.d$qi$pv1)+
					mean(s.op.d$qi$fd)-2*sd(s.op.d$qi$fd),
					mean(s.op.d$qi$pv1)-2*sd(s.op.d$qi$pv1)))
lines(p, open.d.m, type="l", lty="dashed", ylim=c(-1.5, 1.5),
	xlab="", ylab="", lab=c(8,5,4), col="gray")
#Open: Rep
open.r <- array(c(mean(s.op.r$qi$pv1), mean(s.op.r$qi$pv1)+mean(s.op.r$qi$fd)))
lines(p, open.r, type="l", lty="solid", lwd=2, ylim=c(-1.5, 1.5),
		xlab="", ylab="", lab=c(8,5,4))
open.r.p <- array(c(mean(s.op.r$qi$pv1)+2*sd(s.op.r$qi$pv1),
	mean(s.op.r$qi$pv1)+2*sd(s.op.r$qi$pv1)+mean(s.op.r$qi$fd)+2*sd(s.op.r$qi$fd)))
lines(p, open.r.p, type="l", lty="solid", ylim=c(-1.5, 1.5),
	xlab="", ylab="", lab=c(8,5,4), col="gray")
open.r.m <- array(c(mean(s.op.r$qi$pv1)-2*sd(s.op.r$qi$pv1),
	mean(s.op.r$qi$pv1)-2*sd(s.op.r$qi$pv1)+mean(s.op.r$qi$fd)-2*sd(s.op.r$qi$fd)))
lines(p, open.r.m, type="l", lty="solid", ylim=c(-1.5, 1.5),
	xlab="", ylab="", lab=c(8,5,4), col="gray")
abline(h=0, lty="solid")
title(main="Pure Open", font.main=7, cex=1.5)
mtext("Least", side=1, line=.3, cex=.8, at=c(-0.38), font=6)
mtext("Most", side=1, line=.3, cex=.8, at=c(0.35), font=6)
text(-0.3,1.1,"Republicans", cex=.95, adj=c(0,0), font=6)
text(-0.25,-1.2,"Democrats", cex=.95, adj=c(0,1), font=6)
#Nonpartisan: Dem
par(cex.axis=.8, cex.lab=.8, font.axis=6, lheight=.5, mar=c(1,1.5,1.5,.5), mgp=c(1,.75,0),
	fig=c(.36,.68,.1,.53), new=TRUE)
nonpartisan.d <- array(c(mean(s.np.d$qi$pv1)+mean(s.np.d$qi$fd), mean(s.np.d$qi$pv1)))
plot(p, nonpartisan.d, type="l", lty="dashed", lwd=2, ylim=c(-1.5, 1.5),
		xlab="", ylab="", xaxt="n")
nonpartisan.d.p <- array(c(mean(s.np.d$qi$pv1)+2*sd(s.np.d$qi$pv1)+
							mean(s.np.d$qi$fd)+2*sd(s.np.d$qi$fd),
							mean(s.np.d$qi$pv1)+2*sd(s.np.d$qi$pv1)))
lines(p, nonpartisan.d.p, type="l", lty="dashed", ylim=c(-1.5, 1.5),
	xlab="", ylab="", lab=c(8,5,4), col="gray")
nonpartisan.d.m <- array(c(mean(s.np.d$qi$pv1)-2*sd(s.np.d$qi$pv1)+
							mean(s.np.d$qi$fd)-2*sd(s.np.d$qi$fd),
							mean(s.np.d$qi$pv1)-2*sd(s.np.d$qi$pv1)))
lines(p, nonpartisan.d.m, type="l", lty="dashed", ylim=c(-1.5, 1.5),
	xlab="", ylab="", lab=c(8,5,4), col="gray")
#Nonpartisan: Rep
nonpartisan.r <- array(c(mean(s.np.r$qi$pv1), mean(s.np.r$qi$pv1)+mean(s.np.r$qi$fd)))
lines(p, nonpartisan.r, type="l", lty="solid", lwd=2, ylim=c(-1.5, 1.5),
		xlab="", ylab="", lab=c(8,5,4))
nonpartisan.r.p <- array(c(mean(s.np.r$qi$pv1)+2*sd(s.np.r$qi$pv1),
	mean(s.np.r$qi$pv1)+2*sd(s.np.r$qi$pv1)+mean(s.np.r$qi$fd)+2*sd(s.np.r$qi$fd)))
lines(p, nonpartisan.r.p, type="l", lty="solid", ylim=c(-1.5, 1.5),
	xlab="", ylab="", lab=c(8,5,4), col="gray")
nonpartisan.r.m <- array(c(mean(s.np.r$qi$pv1)-2*sd(s.np.r$qi$pv1),
	mean(s.np.r$qi$pv1)-2*sd(s.np.r$qi$pv1)+mean(s.np.r$qi$fd)-2*sd(s.np.r$qi$fd)))
lines(p, nonpartisan.r.m, type="l", lty="solid", ylim=c(-1.5, 1.5),
	xlab="", ylab="", lab=c(8,5,4), col="gray")
abline(h=0, lty="solid")
title(main="Nonpartisan", font.main=7, cex=1.5)
mtext("District Competitiveness", side=1, line=1.8, cex=1.2)
mtext("Least", side=1, line=.3, cex=.8, at=c(-0.38), font=6)
mtext("Most", side=1, line=.3, cex=.8, at=c(0.35), font=6)
text(-0.3,1.3,"Republicans", cex=.95, adj=c(0,0), font=6)
text(-0.25,-1.2,"Democrats", cex=.95, adj=c(0,1), font=6)


